<?php
/**
 * Created by PhpStorm.
 * User: alei
 * Date: 11/5/2017 AD
 * Time: 16:24
 */
namespace App\Models\Mysql;

//use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\BaseDBMethod;

class pre_forum_thread
{
	use BaseDBMethod;
	//数据库连接配置项，见config/database.php
	const DATABASE_CONF = 'lqyw_mysql';
	//pre_ct_ui 首页轮播图所在表
	const TABLE_NAME = 'pre_forum_thread';

	//数据库连接配置项，见config/database.php
	//如果需要用ORM，需要设置此项
//	protected $connection = self::DATABASE_CONF;
	//表名，如果需要用ORM，需要设置此项
//	protected $table = self::TABLE_NAME;

	public static function getThreadInfo($tid)
	{
		return self::getConnObj()->select("SELECT * from pre_forum_thread where tid=".$tid);
	}
	
	public static function selectHomeThreadList($param=[])
	{
		return self::getConnObj()->select(
			"SELECT ct_series.seriesname, difficulty, maxsubmitnumber, submittime, commentendtime,subject,taskid,tid,author,authorid, replies ".
			" FROM pre_ct_task ct_task, pre_forum_thread ft, pre_ct_series ct_series".
			" WHERE ct_task.".$param['whichtid']."=ft.tid AND ct_task.seriesid=ct_series.seriesid".
			" AND ct_task.status=:status ".
			" ORDER BY ".$param['whichorder']." DESC LIMIT 2",
			[
				'status' => $param['ct_task_status']
			]
		);
	}

	public static function selectClassicsAppreciation($param=[])
	{
		return self::getConnObj()->select(
			"select ct_ui.tid, ct_ui.extra1, ft.subject, fp.pid, fp.message".
			" from pre_ct_ui ct_ui,pre_forum_thread ft, pre_forum_post fp ".
			" where ct_ui.tid=ft.tid and ft.tid=fp.tid and first=1 ".
			" and category=:category AND subcategory=:subcategory order by ct_ui.displayorder asc LIMIT 2",
			$param
		);
	}

	#统计总浏览数
    public static function all_browse($param)
    {
        return self::getConnObj()->select(
            "SELECT `views` FROM pre_forum_thread  WHERE tid=:tid ",
            [
                'tid' => $param['tid'],
            ]
        );
    }

    #更新总浏览数

    public static function modify($param)
    {
        $r=self::getConnObj()->update("UPDATE pre_forum_thread SET `views`=`views`+1 WHERE tid=:tid ",
            [
                'tid'=>$param
            ]
            );
        return $r;
    }

    public static function updateAndInc($tid, $updateParam, $incParam){
		$updateList=array();
		foreach($updateParam as $k=>$v){
			$updateList[] = "`".$k."`=".$v;
		}
		foreach($incParam as $v){
			$updateList[] = "`".$v."`=".$v."+1";
		}
		$updateStr = implode(",",$updateList);
		//echo "updateStr = $updateStr </br>";
		
        $r=self::getConnObj()->update("update pre_forum_thread SET ".$updateStr." where tid=".$tid);
		return $r;
	}
	

	
}